<?php
/***************************************************************************
 *   Copyright (C) 2008 by Julian Cromarty  							   *
 *   julian.cromarty@gmail.com 											   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
/**
*
* A class to connect to the database and return a connection object
*
*@author Julian Cromarty
*/
class db	{

/**
*
* A variable to store the database connection
*
*/
private $dbcon;

/**
*
* Constructor. Opens the connection
*
*/
function __construct()	{
	$this->connect();
}

/**
*
* Destructor
*
*/
function __destruct()	{
	$this->close_connection();
}

/**
*
* Connects to the database and checks for connection errors
*
*@return The newly created mysqli connection object on successful connect, 1 otherwise
*/
function connect()
{
	$this->dbcon = new mysqli("localhost", "ehwotay", "uRdEYxbQhXsTNv7Y", "blog");
	if (mysqli_connect_errno()) {
		printf("<p>Connect failed: %s</p>\n", mysqli_connect_error());
		return 1;
	}
	return $this->dbcon;
}

/**
*
* Closes the database connection
*
*/
function close_connection()
{
	$this->dbcon->close();
}

/**
*
* Gets the current database connection
*
*@return The mysqli database connection object if open, else 1
*/
function getInstance()	{
	if($this->dbcon->ping())	{
		return $this->dbcon;
	}
	else	{
		$this->dbcon = $this->connect();
		return $this->dbcon;
	}
}

}	//class db

?>
